package com.example.wisdomclassroomapi.common.util;

import com.example.wisdomclassroomapi.common.enums.ExceptionInfoEnum;
import com.example.wisdomclassroomapi.common.exception.ApplicationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class JsonUtil {
    private final ObjectMapper objectMapper;

    public <T> T read(String content, Class<T> clazz) {
        try {
            return objectMapper.readValue(content, clazz);
        } catch (JsonProcessingException e) {
            throw new ApplicationException(ExceptionInfoEnum.JSON_PROCESSING_EXCEPTION, e);
        }
    }

    public String write(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new ApplicationException(ExceptionInfoEnum.JSON_PROCESSING_EXCEPTION, e);
        }
    }
}
